var vm = new Vue({
	el: '#app',
	data: function() {
		return {
			myFriend: [],
			friendAddMe: [],
			dialogAddFriendVisible: false,
			friendUsername: '',
			myGroup: [],
			dialogCreateGroupVisible: false,
			dialogAddGroupVisible: false,
			selectGroup: {},
			groupMember: [],
			createGroupName: ''
		};
	},
	methods: {
		/**
		 * 查找所有好友（包括关注、黑名单）
		 */
		findFriends: function() {
			var _this = this;
			authAxios({
				method: 'get',
				url: CONST_URL + '/friend/findFriends'
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.myFriend = response.data.data;
				} else {
					_this.$message.error('加载数据失败！');
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},
		/**
		 * 查询自己所在的所有群组
		 */
		findGroupUsers: function() {
			var _this = this;
			authAxios({
				method: 'get',
				url: CONST_URL + '/groupUser/findGroupUsers'
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.myGroup = response.data.data;
				} else {
					_this.$message.error('加载数据失败！');
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},
		/**
		 * 查询所有想添加自己为好友的用户
		 */
		findAddMe: function() {
			var _this = this;
			authAxios({
				method: 'get',
				url: CONST_URL + '/friend/findAddMe'
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.friendAddMe = response.data.data;
				} else {
					_this.$message.error('加载数据失败！');
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},
		/**
		 * 查找用户并添加好友
		 */
		findAndAdd: function() {
			var _this = this;
			_this.findUser().then(function(response) {
				if(response == null) {
					_this.$message.error('未查找到该用户');
					return;
				}
				_this.addFriend(response.id);
			});
		},
		/**
		 * 查找用户
		 */
		findUser: function() {
			var _this = this;
			return axios({
				method: 'get',
				url: CONST_URL + '/user/findUser',
				params: {
					username: _this.friendUsername
				}
			}).then(function(response) {
				if(response.data.code == '0000') {
					return response.data.data;
				} else {
					_this.$message.error(response.data.message);
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},
		/**
		 * 添加好友
		 */
		addFriend: function(id) {
			var _this = this;
			authAxios({
				method: 'post',
				url: CONST_URL + '/friend/add',
				params: {
					id: id
				}
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.findFriends();
					_this.findAddMe();
					_this.dialogAddFriendVisible = false;
					_this.$message.success('添加好友成功！等待对方同意');
				} else {
					_this.$message.error(response.data.message);
					console.log(response.data);
				}
			});
		},
		/**
		 * 取消关注
		 */
		cancelAdd: function(friend) {
			var _this = this;
			this.$confirm('确认取消关注“' + friend.friendNickname + '”吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				authAxios({
					method: 'post',
					url: CONST_URL + '/friend/cancelAdd',
					params: {
						id: friend.friendId
					}
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.findFriends();
						_this.findAddMe();
						_this.$message.success('成功取消关注');
					} else {
						_this.$message.error(response.data.message);
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},
		/**
		 * 删除好友
		 */
		deleteFriend: function(friend) {
			var _this = this;
			this.$confirm('确认删除好友“' + friend.friendNickname + '”吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				authAxios({
					method: 'post',
					url: CONST_URL + '/friend/delete',
					params: {
						id: friend.friendId
					}
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.findFriends();
						_this.findAddMe();
						_this.$message.success('成功删除好友');
					} else {
						_this.$message.error(response.data.message);
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},
		/**
		 * 添加对方为黑名单
		 */
		addBlacklist: function(friend) {
			var _this = this;
			this.$confirm('确认将“' + friend.friendNickname + '”加入黑名单吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				authAxios({
					method: 'post',
					url: CONST_URL + '/friend/addBlacklist',
					params: {
						id: friend.friendId
					}
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.findFriends();
						_this.findAddMe();
						_this.$message.success('成功加入黑名单');
					} else {
						_this.$message.error(response.data.message);
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消黑名单'
				});
			});
		},
		/**
		 * 将对方移除黑名单
		 */
		removeBlacklist: function(friend) {
			var _this = this;
			this.$confirm('确认将“' + friend.friendNickname + '”移除黑名单吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				authAxios({
					method: 'post',
					url: CONST_URL + '/friend/removeBlacklist',
					params: {
						id: friend.friendId
					}
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.findFriends();
						_this.findAddMe();
						_this.$message.success('成功移除黑名单');
					} else {
						_this.$message.error(response.data.message);
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消移除黑名单'
				});
			});
		},
		/**
		 * 同意添加对方为好友（双方互为好友）
		 */
		accept: function(friend) {
			var _this = this;
			authAxios({
				method: 'post',
				url: CONST_URL + '/friend/accept',
				params: {
					id: friend.userId
				}
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.findFriends();
					_this.findAddMe();
					_this.$message.success('成功添加好友');
				} else {
					_this.$message.error(response.data.message);
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},
		/**
		 * 退出群组
		 */
		deleteGroup: function(group) {
			var _this = this;
			this.$confirm('确认退出群“' + group.groupName + '”吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				authAxios({
					method: 'post',
					url: CONST_URL + '/groupUser/delete',
					params: {
						groupId: group.groupId
					}
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.findGroupUsers();
						_this.$message.success('成功退出群组');
					} else {
						_this.$message.error(response.data.message);
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消退出'
				});
			});
		},
		/**
		 * 查找群成员
		 */
		findGroupMember: function(id) {
			var _this = this;
			authAxios({
				method: 'get',
				url: CONST_URL + '/groupUser/findGroupMember',
				params: {
					groupId: id
				}
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.groupMember = response.data.data;
				} else {
					_this.$message.error('加载数据失败！');
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},
		selectAddGroup: function(group) {
			this.selectGroup = group;
			this.findGroupMember(group.groupId);
			this.dialogAddGroupVisible = true
		},
		addGroupUser: function(friend) {
			var _this = this;
			this.$confirm('确认将好友“' + friend.friendNickname + '”加入群“' + _this.selectGroup.groupName + '”吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				authAxios({
					method: 'post',
					url: CONST_URL + '/groupUser/add',
					params: {
						groupId: _this.selectGroup.groupId,
						userId: friend.friendId
					}
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.findGroupMember(_this.selectGroup.groupId);
						_this.$message.success('成功将好友加入群组');
					} else {
						_this.$message.error(response.data.message);
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消'
				});
			});
		},
		createGroup: function() {
			var _this = this;
			if(!isGroupName(_this.createGroupName)) {
				_this.$message.error("群名称由2至20位的中文数字字母或下划线组成");
				return;
			}
			authAxios({
				method: 'post',
				url: CONST_URL + '/group/create',
				params: {
					groupName: _this.createGroupName
				}
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.findGroupUsers();
					_this.dialogCreateGroupVisible = false;
					_this.createGroupName = '';
					_this.$message.success('新建群组成功');
				} else {
					_this.$message.error(response.data.message);
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		}
	},
	mounted: function() {
		this.$nextTick(function() {
			var _this = this;
			checkToken().then(function() {
				var token = localStorage.getItem('token');
				if(token != null) {
					token = JSON.parse(token);
					authAxios.defaults.headers.common['Authorization'] = token.token_type + ' ' + token.access_token;
				}
			}).then(function() {
				_this.findFriends();
				_this.findAddMe();
				_this.findGroupUsers();
			});
		});
	}
});